package com.cdfahe.gms.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cdfahe.common.util.Md5Encode;
import com.cdfahe.common.util.StringUtils;
import com.cdfahe.gms.aop.SysContent;
import com.cdfahe.gms.mybatis.entity.UserFh;
import com.cdfahe.gms.service.ISystemManageService;
import com.cdfahe.gms.service.UserService;


/**
 * 登录控制类
 * @author  roy xiao11
 * @version 
 * @since   JDK1.6
 */
@Controller("loginController")
public class LoginController {
	
	@Autowired
    private ISystemManageService iSystemService;
	@Autowired
    private UserService userService;
	
	@RequestMapping(value = "/regist")
    public String toregest(Model model) {
		return "user/regist";
    }
	
	/**
	 * 注册会员控制器
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/doregist")
    public String doadduser(HttpServletRequest request) {
		UserFh user = new UserFh();
		String userid = StringUtils.trim(request.getParameter("userid"));
		String userpass = StringUtils.trim(request.getParameter("userpass"));
		String usertruename = StringUtils.trim(request.getParameter("usertruename"));
		if (!StringUtils.isEmpty(userid) && !StringUtils.isEmpty(userpass)) {
			user.setEmail(userid);
			user.setPass(Md5Encode.encode(StringUtils.trim(userpass)));
			user.setName(usertruename);
			user.setLevel(1);
		}
		
		userService.addUser(user);
		
		return "user/registsuccess";
    }
	
	/**
	 * 登录跳转页面
	 * @param model
	 * @return ;
	 */
    @RequestMapping(value = "/login")
    public String index(Model model) {
    	if (SysContent.getRequest().getSession().getAttribute("LOGINSTATUS") != null && SysContent.getRequest().getSession().getAttribute("LOGINSTATUS").equals("1")) {
    		// 如果此属性不为空，说明用户是通过某个链接直接访问的系统，在登录后，需要直接转向链接页面
        	if (SysContent.getRequest().getSession().getAttribute("ACCESSSITE") != null) {
//        		try {
//        			System.out.println("--------into Login-----------------");
////    				SysContent.getResponse().sendRedirect((String) SysContent.getRequest().getSession().getAttribute("ACCESSSITE"));
//    			} catch (IOException e) {
//    				e.printStackTrace();
//    			}
        	} else {
        		return "index";
        	}
    	} else {
    		return "login";
    	}
    	return null;
    	
    }
    
    
    @RequestMapping(value = "/logout")
    public String logout(Model model) {
    	System.out.println("--------out Login -----------------");
    	if (SysContent.getRequest().getSession().getAttribute("LOGINSTATUS") != null && SysContent.getRequest().getSession().getAttribute("LOGINSTATUS").equals("1")) {
    		SysContent.getRequest().getSession().setAttribute("LOGINSTATUS",null);
    		SysContent.getRequest().getSession().setAttribute("username","null");
    		SysContent.getRequest().getSession().setAttribute("userid","null");
    		SysContent.getRequest().getSession().setAttribute("level","0");
    	}

//    	if (SysContent.getRequest().getSession().getAttribute("LOGINSTATUS") != null && SysContent.getRequest().getSession().getAttribute("LOGINSTATUS").equals("1")) {
//    		// 如果此属性不为空，说明用户是通过某个链接直接访问的系统，在登录后，需要直接转向链接页面
//        	if (SysContent.getRequest().getSession().getAttribute("ACCESSSITE") != null) {
//        		try {
//    				SysContent.getResponse().sendRedirect((String) SysContent.getRequest().getSession().getAttribute("ACCESSSITE"));
//    			} catch (IOException e) {
//    				e.printStackTrace();
//    			}
//        	} else {
//        		return "mypage";
//        	}
//    	} else {
//    		return "login";
//    	}
    	return "index";
    	
    }
    
    
    /**
     * 验证用户名和密码的有效性
     * @param request
     * @return
     */
    @RequestMapping(value = "/ajaxlogin")
    @ResponseBody
    public String ajaxindex(HttpServletRequest request) {
    	String username = request.getParameter("username");
    	String password = request.getParameter("password");
    	List<UserFh> list = iSystemService.login(username, Md5Encode.encode(password));
    	if (list.size() > 0) {
    		request.getSession().setAttribute("LOGINSTATUS", "1");
    		request.getSession().setAttribute("userid", list.get(0).getId());
    		request.getSession().setAttribute("level", list.get(0).getLevel());
    		request.getSession().setAttribute("username", username);
    		return "1";
    	} else {
    		return "0";
    	}
    }
}
